<?php
include ('../../__global.php');
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_user.php');
class aj_applyShop extends ml_controller {
    
    protected $nick;
    function initParam(){
        
        $this->nick = $this->input('nick', '', '');
        $this->tags=$this->input('tags','','');
        $this->cate=$this->input('cate','','');
        $this->isShare=$this->input('isShare','',false);

        $this->uid=$this->__visitor['uid'];
    }
    
    function checkParam(){

        if(!$this->check_permission(ML_PERMISSION_LOGIN_ONLY)) {
            $this->api_output(ML_RCODE_NOLOGIN);
        }
        else if(!$this->check_permission(ML_PERMISSION_LOGIN_CANWRITE)) {
            $this->api_output(ML_RCODE_NOACTIVE);
        }
        //检查nick
        if(empty($this->nick)){
            $this->api_output(ML_RCODE_PARAM,'','empty nick');
        }

        if(empty($this->cate)){
            $this->api_output(ML_RCODE_PARAM,'','empty cate');
        }

        if(!Tool_keyword::check_article_kill_keyword($this->nick,'utf8')){
            return $this->api_output(ML_RCODE_PARAM,'nick sensitive_word');
        }
        if(!preg_match(ML_DATARULE_USER_NICK_PREG , $this->nick))
            $this->api_output(ML_RCODE_PARAM ,'', 'nick wrong');
        $len = Tool_string::str_width($this->nick);
        if($len < ML_DATARULE_USER_NICK_MINLEN || $len > ML_DATARULE_USER_NICK_MAXLEN)
            $this->api_output(ML_RCODE_PARAM ,'','nick length');


        //检查tags
        if(!empty($this->tags)){
            $this->tagArr =explode(';',$this->tags);
            foreach($this->tagArr as $val){
                $val=Tool_keyword::filterSensitiveWord($val);
                if(Tool_string::str_width($val) > ML_DATARULE_USER_SKILLTAGS_MAXLEN ){
                    $this->api_output(ML_RCODE_PARAM,'','tags too long');
                }
            }
        }
    }

    public function updateNick(){

        $oAccount = new ml_model_dbUserAccount();
        $oAccount->isNickExists($this->nick,$isExists);
        if($isExists){
            $this->api_output(ML_RCODE_PARAM,'','nick exist');
        }

        $accountRes = $oAccount->updateNick($this->uid, $this->nick);
        if(false === $accountRes){
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        }
        $this->loginProxy( modify_login, array('nickname'=>$this->nick));
        $rdsHash=new ml_model_rdsHash();
        $rdsHash->updateNick($this->uid, $this->nick);
        ml_tool_sendMq::update_atnick_nick($this->nick, $this->uid);
    }


    public function send_weibo(){

        $o3rd = new ml_model_db3rdService();
        $rs = $o3rd -> get3rdServiceUser($this->uid,1 ,$serData);
        if(!$rs){
            return false;
        }
        $wb['access_token']=$serData['access_token'];

        $wb['content']='我已申请美啦好店，你也来看看吧！——http://meila.com/page/shop/bind_shop.php';
        $service_id = 1; //weibo
        ml_tool_sendMq::send_3rdweibo($service_id,$wb);
    }


    function main(){

        $nickname = $this->__visitor['nickname'];
        if($nickname != $this->nick){
            $this->updateNick();
        }

        if( !empty($this->tags) && count($this->tagArr)>0  ){
            $this->updateSkillTags();
        }

        $insert=array();
        $insert['uid']=$this->uid;
        $session=self::getSession();
        $this->tao_nick=$session->getVal('3rd_nick');
        $insert['tao_nick']=$this->tao_nick;
        $insert['tags']=$this->tagArr;
        $insert['cate']=$this->cate;
        if($this->isShare){
            $this->send_weibo();
        }
        //投后台队列
        ml_tool_sendMq::add_apply_shop($insert);
        $this->api_output(ML_RCODE_SUCC);
    }

        public function updateSkillTags(){

        $obrief=new ml_model_dbUserBriefInfo();
        $obrief->getSkillTags($this->uid);
        $oldTagArr = $obrief->get_data();
        if(count($oldTagArr['skill_tags'])>0){
            $newTags=array_unique(array_merge($oldTagArr['skill_tags'],$this->tagArr));
        }else{
            $newTags=$this->tagArr;
        }
        $rs=$obrief->updateSkillTags($this->uid,$newTags);
        if(!rs){
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        }
    }

}

new aj_applyShop();
